//
//  MyJSObject.m
//  JSTest
//
//  Created by BAP_0006 on 2018/4/9.
//  Copyright © 2018年 BAP_0006. All rights reserved.
//

#import "MyJSObject.h"

#import <JavaScriptCore/JavaScriptCore.h>

#import <UIKit/UIKit.h>

@protocol PersonJSExport <JSExport>

//- (void)nslog:(NSString *)str;

//JSExportAs(nslog,-(void)printSome:(NSString *)string);

JSExportAs(nslog,-(void)printWithString:(NSString *)string callback:(NSString *)callback);

@end

@interface MyJSObject()<PersonJSExport>

@property (nonatomic, strong)UIWebView *webView;

@end

@implementation MyJSObject

- (instancetype)initWithWebView:(UIWebView *)webView{
    if (self = [super init]) {
        _webView = webView;
    }
    return self;
}

+ (instancetype)objectWithWebView:(UIWebView *)webView{
    return [[self alloc] initWithWebView:webView];
}

-(void)printWithString:(NSString *)string callback:(NSString *)callback{
    NSLog(@"%@",string);
    NSString *callbackJS = [NSString stringWithFormat:@"%@('我是callback')",callback];
    //1秒后执行js回调方法
    [self.webView performSelector:@selector(stringByEvaluatingJavaScriptFromString:) withObject:callbackJS afterDelay:1];
}

- (void)nslog:(NSString *)str{
    NSLog(@"%@",str);
}

- (void)printSome:(NSString *)string{
    NSLog(@"%@",string);
}

@end
